﻿#region Using Statements

using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

#endregion

namespace GameName1
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        private static int RESOLUTION_X = 800;
        private static int RESOLUTION_Y = 600;

        private Level level = new Level();
        private XnaSpaceShipDecorator spaceShip;
        private List<XnaAngryAlienDecorator> aliensList = new List<XnaAngryAlienDecorator>();
        

        private Texture2D backgroundSprite;
        private Texture2D barSprite;
        private Texture2D playerSprite;
        private Texture2D[] enemySprite = new Texture2D[6];
        private Texture2D rocketSprite;
        private Texture2D alienExploding;

        public Game1()
            : base()
        {         
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            graphics.PreferredBackBufferWidth = RESOLUTION_X;
            graphics.PreferredBackBufferHeight = RESOLUTION_Y;

            spaceShip = level.SpaceShip;
            aliensList = level.Aliens;
            IsMouseVisible = true;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            playerSprite = Content.Load<Texture2D>("Sprites/player.png");
            enemySprite[0] = Content.Load<Texture2D>("sprites/invader1");
            enemySprite[1] = Content.Load<Texture2D>("sprites/invader2");
            enemySprite[2] = Content.Load<Texture2D>("sprites/invader3");
            enemySprite[3] = Content.Load<Texture2D>("sprites/invader1_a");
            enemySprite[4] = Content.Load<Texture2D>("sprites/invader2_a");
            enemySprite[5] = Content.Load<Texture2D>("sprites/invader3_a");
            rocketSprite = Content.Load<Texture2D>("sprites/projectile");
            alienExploding = Content.Load<Texture2D>("sprites/explosion.png");

            backgroundSprite = Content.Load<Texture2D>("sprites/background");
            barSprite = Content.Load<Texture2D>("sprites/bar.png");

            spaceShip.Sprite = playerSprite;
            foreach (var alien in aliensList)
            {
                alien.Sprite = enemySprite[0];
            }
            Rocket.sprite = rocketSprite;
            XnaAngryAlienDecorator.AlienExploding = alienExploding;

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            KeyboardState keyState = Keyboard.GetState();
            spaceShip.ControlObject(keyState);

            foreach (XnaProjectileDecorator projectile in level.Projectiles)
            {
                projectile.Move();
            }
            // TODO: Add your update logic here

            level.CheckColission();
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();
            spriteBatch.Draw(backgroundSprite,new Vector2(0,0),Color.White);
            spriteBatch.Draw(barSprite,new Vector2(20,RESOLUTION_Y-15));
            spriteBatch.Draw(spaceShip.Sprite,spaceShip.Position,Color.White);
            foreach (var alien in aliensList)
            {
                spriteBatch.Draw(alien.Sprite,alien.Position,Color.White);
            }

            foreach (XnaProjectileDecorator projectile in level.Projectiles)
            {
                spriteBatch.Draw(projectile.Sprite, projectile.Position, Color.White);
            }
            // TODO: Add your drawing code here
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
